package iPersistence.utils;

public class GenericTokenParser {
    private final String openToken;
    private final String closeToken;
    private final TokenHandler handler;

    public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
        this.openToken = openToken;
        this.closeToken = closeToken;
        this.handler = handler;
    }

    /**
     * 解析#{}
     *
     * @param sql
     * @return
     */
    public String parse(String sql) {
        if (sql == null || sql.isEmpty()) {
            return "";
        }

        int start = sql.indexOf(openToken);
        if (start == -1) {
            return sql;
        }
        char[] src = sql.toCharArray();
        int offset = 0;
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;

        while (start > -1) {
            // 判断openToken前是否有转义字符
            if (start > 0 && src[start - 1] == '\\') {
                // 截取字符数组中偏移量到openToken之间的字符，由于openToken前有转义字符，此处直接接上openToken
                builder.append(src, offset, start - offset - 1).append(openToken);
                // 偏移到openToken后
                offset = start + openToken.length();
            } else {
                // 重置expression
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                // 截取字符数组中偏移量到openToken前的位置
                builder.append(src, offset, start - offset);
                // 偏移到openToken后
                offset = start + openToken.length();
                int end = sql.indexOf(closeToken, offset);
                while (end > -1) {
                    // 判断closeToken前是否有转义字符
                    if (end > offset && src[end - 1] == '\\') {
                        // 截取字符数组中偏移量到closeToken之间的字符，由于closeToken前有转义字符，此处直接接上closeToken
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        // 偏移量偏移到closeToken后
                        offset = end + closeToken.length();
                        // 继续查找closeToken
                        end = sql.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        offset = end + closeToken.length();
                        break;
                    }
                }
                if (end == -1) {
                    //找不到closeToken则直接截取剩余所有字符
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    // 调用tokenHandler处理占位符中的表达式
                    builder.append(handler.handleToken(expression.toString()));
                    offset = end + closeToken.length();
                }
            }
            start = sql.indexOf(openToken, offset);
        }
        // 接上剩余的所有字符
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }
}
